package com.wesley.growth.quartz.config;

import com.wesley.growth.quartz.job.FirstJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * JobInit
 *
 * @author WangPanYong
 * @since 2021/04/12 15:38
 */
@Component
public class JobInit implements ApplicationRunner {

    public static final String ID = "SUMMERDAY";

    @Autowired
    private Scheduler scheduler;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        JobDetail jobDetail = JobBuilder.newJob(FirstJob.class)
                                        .withIdentity(ID + "01")
                                        .withDescription("jobDesc")
                                        .storeDurably()
                                        .build();
        CronScheduleBuilder scheduleBuilder =
                CronScheduleBuilder.cronSchedule("0/5 * * * * ? *");
        // 创建任务触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                                        .forJob(jobDetail)
                                        .withIdentity(ID + "01Trigger")
                                        .withSchedule(scheduleBuilder)
                                        .startNow() //立即執行一次任務
                                        .build();
        // 手动将触发器与任务绑定到调度器内
        scheduler.scheduleJob(jobDetail, trigger);
    }


}
